package com.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.im.dao.UserDao;
import com.im.pojo.ImUser;
import com.im.pojoClass.Result;
import com.im.pojoClass.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@Api(tags = {"其他测试"}, value = "OtherController")
@RestController
@CrossOrigin
public class OtherController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "清空数据库")
    @RequestMapping(value = "/delete_mysql", method = RequestMethod.POST)
    public void delete_mysql() {
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
        Set<String> keys1 = stringRedisTemplate.keys("*");
        stringRedisTemplate.delete(keys1);

        userDao.truncate();
    }

    @ApiOperation(value = "测试延迟")
    @RequestMapping(value = "/delay", method = RequestMethod.GET)
    public Result delay(String s) {
        System.out.println(s);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Object test() {
        ImUser imUser = new ImUser("a", "b", "c", "d");

//        ValueOperations<String, String> stringValueOperations = stringRedisTemplate.opsForValue();
//        stringValueOperations.set("im1", String.valueOf(imUser));
//        String im = stringValueOperations.get("im1");
//        System.out.println(im);

        String s = JSONObject.toJSONString(imUser);
        stringRedisTemplate.opsForValue().set("im2", s);
        String imUser1 = stringRedisTemplate.opsForValue().get("im2");
        ImUser o = JSONObject.parseObject(imUser1, ImUser.class);

        System.out.println(o);
        System.out.println(o.getUser_id());

        return "1";
    }
}
